package com.yjps.house.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yjps.common.message.Message;
import com.yjps.common.model.Address;
import com.yjps.common.util.TimeUtil;
import com.yjps.house.model.Building;
import com.yjps.house.model.House;
import com.yjps.house.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jelly on 2015/8/25 0025.
 */

@Controller
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    BuildingService buildingService;

    /**
     * 获取楼盘信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String getList() {
        Message message;
        List<Building> buildingsList = buildingService.getList();
        if(!buildingsList.isEmpty()){
            //String jsonString = JSON.toJSONString(buildingsList);
            //System.out.println(jsonString);
            //Map<String,Object> map=new HashMap<>();
            //map.put("buildingsList",buildingsList);
            message = new Message(true,"成功获取楼盘列表",buildingsList);
        }else {
            message = new Message(false,"获取楼盘列表失败");
        }

        return message.toJson();
    }

    /**
     * 删除楼盘信息
     *
     * @param  buildingId
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam(value = "buildingId", required = true) String buildingId) {
        int buildingID=Integer.parseInt(buildingId);
        Message message;
        if (buildingID<=0){
            message=new Message(false,"参数错误");
        }else{
            buildingService.delete(buildingID);
            message=new Message(true,"删除楼盘成功");
        }
        return message.toJson();
    }

    /**
     * 添加楼盘信息
     *
     * @param json "address":{ "province": "", "city": "", "country": "","town": "", "detail": "" },"building": { "agentsId": "", "title": "", "description": "", "imageUrl": "", "averagePrice": "", "houseType": "","addressId": "" }
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@RequestParam(value = "building") String json) {
        //解析json
        JSONObject object = JSON.parseObject(json);

        Address address = JSON.parseObject(object.getString("address"),Address.class);
        int addressId = buildingService.addAddress(address);

        //int agentsId = Integer.parseInt(object.getString("agentsId"));

        //String time = TimeUtil.getCurrentTimeInString(TimeUtil.DATE_FORMAT_DATE);
        Building building = JSON.parseObject(object.getString("building"), Building.class);

        //调用业务
        int buildingId = buildingService.add(building,addressId);

        //封装返回
        Message message;
        Map<String,Object> map=new HashMap<>();
        if (buildingId>0){
            map.put("buildingId",buildingId);
            message=new Message(true,"发布房源成功",map);
        }else {
            message=new Message(false,"发布房源失败");
        }
        return message.toJson();
    }

}
